﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKInspectModel.ModelValidation.Utils;

namespace YJKInspectModel.ModelValidation.Analyser
{
	public class Node2D
	{
		public XYZ Position
		{
			get
			{
				return this.m_ptPosition;
			}
		}

		public List<Pole2D> PoleList
		{
			get
			{
				return this.m_PoleList;
			}
		}

		public void AddPole(Pole2D pole, bool resort)
		{
			this.m_PoleList.Add(pole);
			if (resort)
			{
				this.AnticlockwiseSortElements();
			}
		}

		public void RemoveEdge(int index, bool resort)
		{
			if (index >= 0 && index < this.m_PoleList.Count)
			{
				this.m_PoleList.RemoveAt(index);
				if (resort)
				{
					this.AnticlockwiseSortElements();
				}
			}
		}

		public void AnticlockwiseSortElements()
		{
			this.m_PoleList.Sort(new AngleComparerByNode(this.m_ptPosition, 0.0));
		}

		public void ClockwiseSortElements()
		{
			this.m_PoleList.Sort(new AngleComparerByNode(this.m_ptPosition, 0.0));
			this.m_PoleList.Reverse();
		}

		public int FindLinkPoleWithOtherNode(XYZ ptOtherNode, Node2D.SearchType searchType)
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.m_PoleList.Count; i++)
			{
				XYZ xyz = this.m_PoleList[i].CurEndPoint(this.m_ptPosition);
				if (xyz != null && Geometry.IsEqual(ptOtherNode, xyz, 0.0001))
				{
					list.Add(i);
				}
			}
			if (list.Count < 1)
			{
				return -1;
			}
			if (list.Count == 1)
			{
				return list[0];
			}
			int result = -1;
			int result2 = -1;
			double val = double.MaxValue;
			double val2 = double.MinValue;
			foreach (int num in list)
			{
				double num2 = 0.0;
				this.m_PoleList[num].GetWinding(this.m_ptPosition, ref num2);
				if (Geometry.LessThan(num2, val))
				{
					val = num2;
					result = num;
				}
				if (Geometry.GreaterThan(num2, val2))
				{
					val2 = num2;
					result2 = num;
				}
			}
			if (searchType == Node2D.SearchType._Anticlockwise)
			{
				return result;
			}
			return result2;
		}

		public Node2D(Node2D rhs)
		{
			this.m_ptPosition = rhs.m_ptPosition;
			this.m_PoleList = rhs.m_PoleList;
		}

		public Node2D(XYZ ptPosition, List<Pole2D> poleList)
		{
			this.m_ptPosition = ptPosition;
			this.m_PoleList = poleList;
		}

		private XYZ m_ptPosition;

		private List<Pole2D> m_PoleList = new List<Pole2D>();

		public enum SearchType
		{
			_Anticlockwise,
			_Clockwise
		}
	}
}
